package com.reader.cms.controller.sys;


import com.baomidou.mybatisplus.plugins.Page;
import com.reader.cms.entity.sys.SysUser;
import com.reader.cms.mapper.sys.SysUserMapper;
import com.reader.cms.service.sys.ISysUserService;
import com.reader.cms.controller.BaseController;
import com.reader.core.entity.ResultMessageInfo;
import com.reader.core.exception.CustomException;
import com.reader.core.utils.CommonVariable;
import com.reader.core.utils.EncryptUtil;
import com.reader.core.utils.ValidatorUtil;
import com.reader.core.validator.group.AddGroup;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jiahaikun
 * @since 2018-06-27
 */
@RestController
@RequestMapping("/api/sysuser")
public class SysUserController extends BaseController<Object> {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    SysUserMapper sysUserMapper;

   @GetMapping(value="getuserinfo")
   @ResponseBody
   //@RequiresPermissions("tenant:dateQuery1")
    public ResultMessageInfo getUserInfo(){

       SysUser userParam=new SysUser();
       userParam.setLoginId(this.getLoginId());
      // SysUser sysUserInfo=iSysUserService.getUserInfo(userParam);
       SysUser  sysUser=iSysUserService.getUserInfo(userParam);
       /* if(sysUserInfo.getRoles().size() == 0){
            throw new CustomException("ERR_11006");
        }*/
        System.out.println(sysUser.toString());

//        ResultMessageInfo resultMessage=this.getSuccessResultMessage("MSG_10007",sysUser);

        return ResultMessageInfo.ofSuccess("查询成功！",sysUser);

    }
    /**
     * @function 分页查询数据
     *
     * @param currentPage   当前页
     * @param size   每页记录数
     * @param loginId   用户名
     * @param name   姓名
     * @return
     */
    @GetMapping("list")
    public ResultMessageInfo selectListPage(@RequestParam(value = "currentPage") int currentPage ,
                                                    @RequestParam(value = "size") int size,
                                                    @RequestParam(value = "loginId",required = false) String loginId,
                                                    @RequestParam(value = "name",required = false) String name,
                                                    @RequestParam(value = "code",required = false) String code) {

        ResultMessageInfo resultMessage=null;
        Page<Map<String, Object>> list=iSysUserService.selectListPage(currentPage,size,loginId,name,code);
//        resultMessage = this.getSuccessResultMessage("MSG_10007",list);
        return ResultMessageInfo.ofSuccess("查询成功！",list);
    }
    @PostMapping("configUserRoles")
    public ResultMessageInfo configUserRoles(@RequestBody  SysUser sysUser) {
        ResultMessageInfo resultMessage=null;
        iSysUserService.configUserRoles(sysUser);
//        resultMessage = this.getSuccessResultMessage("MSG_10012", "");
        return ResultMessageInfo.ofSuccess("角色配置成功！");
    }

    /**
     * @function 添加新记录
     * @param sysUser 实体对象
     * @return
     */
    @PostMapping("insert")
    public ResultMessageInfo insert(@RequestBody  SysUser sysUser) {
        ValidatorUtil.validateEntity(sysUser, AddGroup.class);//非空验证
        SysUser u = new SysUser();
        u.setLoginId(sysUser.getLoginId());
        SysUser user = sysUserMapper.selectOne(u);
        if(user != null){
            throw new CustomException("ERR_10011");
        }
        ResultMessageInfo resultMessage=null;
        String sha256Password= EncryptUtil.encryptPassword(
                CommonVariable.ENCRYTP_ALGORITHMNAME,
                sysUser.getPassword(),
                CommonVariable.ENCRYTP_SALT,
                CommonVariable.ENCRYTP_ITERATIONS);
        sysUser.setPassword(sha256Password);
        sysUser.setDelFlag(0);
        Integer userId = this.getUserId();
        sysUser.setInsertDt(new Date());
        sysUser.setInsertUserId(userId);
        sysUser.setUpdtDt(new Date());
        boolean resultValue =  iSysUserService.insert(sysUser);
        if (!resultValue) {
            resultMessage = ResultMessageInfo.ofMessage("添加失败！");
        }
        return ResultMessageInfo.ofSuccess("添加成功！");
    }

    /**
     * @function 修改
     * @param sysUser 实体对象
     * @return
     */
    @PostMapping("update")
    @Transactional
    @ApiOperation(value="跟据ID列新记录", notes="id为必须项")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "要更新对象ID", required = true, dataType = "int", paramType = "path")})
    public ResultMessageInfo update(@RequestBody  SysUser sysUser) {
        ValidatorUtil.validateEntity(sysUser, AddGroup.class);//非空验证
        ResultMessageInfo resultMessage=null;
        Integer userId = this.getUserId();
        sysUser.setUpdtUserId(userId);
        sysUser.setUpdtDt(new Date());
        boolean result = iSysUserService.updateUser(sysUser);
        if(!result){
            resultMessage = ResultMessageInfo.ofMessage("修改失败！");
        }
        return ResultMessageInfo.ofSuccess("修改成功！");
    }

    /**
     * @function 删除
     * @param sysUser 实体对象
     * @return
     */
    @PostMapping("delete_user")
    @Transactional
    @ApiOperation(value="跟据ID列新记录", notes="id为必须项")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "要更新对象ID", required = true, dataType = "int", paramType = "path")})
    public ResultMessageInfo delete(@RequestBody  SysUser sysUser) {
        ResultMessageInfo resultMessage=null;
        Integer userId = this.getUserId();
        sysUser.setUpdtUserId(userId);
        sysUser.setUpdtDt(new Date());
        sysUser.setDelFlag(1);
        boolean result = iSysUserService.deleteUser(sysUser);
        if(!result){
            resultMessage = ResultMessageInfo.ofMessage("删除失败！");
        }
        return ResultMessageInfo.ofSuccess("删除成功！");
    }

}

